package com.atguigu.controller;

import com.atguigu.mapper.DepartmentMapper;
import com.atguigu.mapper.EmployeeMapper;
import com.atguigu.pojo.Department;
import com.atguigu.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.spring6.util.SpringReactiveModelAdditionsUtils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Component
@Controller
public class EmployeeController {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @RequestMapping("/emps")
    public String getAllEmpts(Model model){
        List<Employee> allEmpts = employeeMapper.getAllEmpts();
     model.addAttribute("emps", allEmpts);
     return "/emp/list";
    }
    @RequestMapping("/queryeempByUsername")
    public String queryeempByUsername(@RequestParam("username") String username,Model model){
        Employee emptByName = employeeMapper.getEmptByName(username);
        model.addAttribute("emps",emptByName);
        return "/emp/list";

    }
    @GetMapping("/addemp")
    public String toaddemp(Model model){
        List<Department> allDepts = departmentMapper.getAllDepts();
        model.addAttribute("depts",allDepts);
        return "/emp/add";
    }
    @PostMapping("/addemp")
    public String addemp(
                        @RequestParam("name")String name,
                        @RequestParam("password") String password,
                        @RequestParam("email")String email,
                        @RequestParam("gender") int gender,
                        @RequestParam("department") String department,
                        @RequestParam("birth") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate birth
    ){
        // 打印收到的参数，检查是否为空或格式不正确
//        System.out.println("name: " + name);
//        System.out.println("password: " + password);
//        System.out.println("email: " + email);
//        System.out.println("gender: " + gender);
//        System.out.println("department: " + department);
//        System.out.println("birth: " + birth);
        Employee employee = new Employee(name, password, email, gender, department, birth);

//        System.out.println(employee);
        employeeMapper.addEmpts(employee);

        return "redirect:/emps";
    }
    @GetMapping("/updateemp/{id}")
    public String toupdateEmpt(@PathVariable int id,Model model){
        Employee emptsById = employeeMapper.getEmptsById(id);
        model.addAttribute("emp",emptsById);

        return "/emp/update";
    }
    @PostMapping("/updateEmp")
    public String updateEmpt(@RequestParam("id") int id,
                            @RequestParam("password") String password,
                            @RequestParam("name")String name,
                            @RequestParam("email")String email,
                            @RequestParam("gender") int gender,
                            @RequestParam("department") String department,
                            @RequestParam("birth") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate birth
                            ){

        Employee employee = new Employee(id,password,name, email, gender, department, birth);
        employeeMapper.updateeEmpts(employee);
        return "redirect:/emps";
    }
    @RequestMapping("/deleteemp/{id}")
    public String deleteEmpt(@PathVariable int id){
        employeeMapper.deleteEmpts(id);
        return "redirect:/emps";
    }

}
